package com.eshop.service;

import com.eshop.mapper.GoodMapper;
import com.eshop.pojo.Good;
import com.eshop.pojo.RespPageBean;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodService {
    @Autowired
    GoodMapper goodMapper;

    //查询商品列表
    public List<Good> queryGoodList(Good good){
        return goodMapper.queryGoodList(good);
    }

    //按照商品编号查询
    public Good queryGoodById(int id){
        return goodMapper.queryGoodById(id);
    }

    //冻结商品
    public int freezeGood(int id){
        return goodMapper.freezeGood(id);
    }

    //解冻商品
    public int recoverGood(int id){
        return goodMapper.recoverGood(id);
    }

    //撤下商品，已销售
    public int removeGood(int id){
        return goodMapper.removeGood(id);
    }

    //查看历史销售商品
    public List<Good> findSellGoodList(){
        return goodMapper.findSellGood();
    }

    //删除商品
    public int delGood(int id){
        return goodMapper.delGoodById(id);
    }

    //发布商品
    public int addGood(Good good){
        good.setGIsFreeze(0);
        good.setGIsSell(0);
//        good.setGImgUrl("https://images.weserv.nl/?url="+good.getGImgUrl());
        return goodMapper.addGood(good);
    }

    //分页获取商品列表
    public RespPageBean getGoodListByPage(Integer currentPage, Integer size, Good good) {
        PageHelper.startPage(currentPage,size);
        List<Good> goodList =  goodMapper.getGoodListByPage(good);
        PageInfo<Good> data = new PageInfo<Good>(goodList);

        //将数据封装到RespPageBean的实例对象bean中并返回给Controller层
        RespPageBean bean = new RespPageBean();
        bean.setData(data.getList());
        bean.setTotal(data.getTotal());
        return bean;
    }
    //更新商品信息
    public int updateById(Good good) {
        return goodMapper.updateById(good);
    }
    //分页获取已冻结商品列表
    public RespPageBean getFreezedGoodByPage(Integer currentPage, Integer size, Good good) {
        PageHelper.startPage(currentPage,size);
        List<Good> goodList =  goodMapper.getFreezedGoodByPage(good);
        PageInfo<Good> data = new PageInfo<Good>(goodList);
        RespPageBean bean = new RespPageBean();
        bean.setData(data.getList());
        bean.setTotal(data.getTotal());
        return bean;
    }
    //分页获取已下架商品列表
    public RespPageBean getHistoryGoodByPage(Integer currentPage, Integer size, Good good) {
        PageHelper.startPage(currentPage,size);
        List<Good> goodList =  goodMapper.getHistoryGoodByPage(good);
        PageInfo<Good> data = new PageInfo<Good>(goodList);
        RespPageBean bean = new RespPageBean();
        bean.setData(data.getList());
        bean.setTotal(data.getTotal());
        return bean;
    }

}
